<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
require_once 'admin-load.php';

$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'index';

/*
 * list all phone
 */
switch ($action) {
    case 'index':
        index();
        break;
    case 'add':
        add();
        break;

    case 'delete':
        delete();
        break;

    case 'edit':
        edit();
        break;
}
/**
 *
 * @global type $twig
 * @global ezSQL_mysql $dbp 
 */
function add() {
    global $twig, $dbp;
    $brand = $dbp->get_results("SELECT id, name FROM brand");
    
    $detail_options = get_option_child(get_option_id('phone_detail'));
    
    if (isset($_POST['save']) && $_POST['save'] == 1) {
        
        $details = $_POST['detail'];
        
        $model = $dbp->escape($_POST['model']);
        $price = $dbp->escape($_POST['price']);
        $status = $dbp->escape($_POST['status']);
        $release = $dbp->escape($_POST['release']);
        $brand = $dbp->escape($_POST['brand']);
        $user = current_user();
        $creat = date('Y-m-d h:i:s');
        $promotion = $dbp->escape($_POST['promotion']);
        $remark = $dbp->escape($_POST['remark']);
        $photo = $_FILES['photo'];
        
        $photo_name = '';
        if ($photo['error'] == 0) {
            $photo_name = upload_image($photo, PICTURE_PHONE);
        }
        
        $query = "INSERT INTO phone (model, price, status, `release`, brand_id, user_id, created, promotion, remark, photo) VALUES('$model', '$price', '$status', '$release', '$brand', '$user->id', '$creat', '$promotion', '$remark', '$photo_name')";
        if ($dbp->query($query)) {
            save_phone_detail($dbp->insert_id, $details);
            //redirect(BASE_URL . 'admin/phone.php');
        }
    }
    echo $twig->render('phone-add.html', array('brandname' => $brand, 'detail_options' => $detail_options));
}

function edit(){
    global $twig, $dbp;

        if (isset($_GET['phone_id'])) {
            $brand = $dbp->get_results("SELECT id, name FROM brand");
            $id = intval($_GET['phone_id']);
            $select = "SELECT p.id, p.model, p.price, p.status, p.release, p.brand_id, p.promotion, p.remark, b.name as brand FROM phone p,  brand b WHERE p.id = $id && p.brand_id=b.id ";
            $phone = $dbp->get_row($select);

            echo $twig->render('phone-edit.html', array('phone' => $phone, 'brandname' => $brand));
        }

        if (isset($_POST['edit']) AND $_POST['edit'] == 1) {
            $id = $_POST['phone_id'];
            $model = $dbp->escape($_POST['model']);
            $price = $dbp->escape($_POST['price']);
            $status = $dbp->escape($_POST['status']);
            $release = $dbp->escape($_POST['release']);
            $brand = $dbp->escape($_POST['brand']);
            $user = current_user();
            $modified = date('Y-m-d h:i:s');
            $promotion = $dbp->escape($_POST['promotion']);
            $remark = $dbp->escape($_POST['remark']);
            $photo = $_FILES['photo'];
            
            $photo_name = '';
            if($photo['error']==0){
                $photo_name = upload_image($photo, PICTURE_PHONE);
            }
            $update = "UPDATE phone SET model='$model', price='$price', status='$status', `release`='$release', brand_id = '$brand', user_id='$user->id', `modified`='$modified', promotion='$promotion', remark='$remark', photo='$photo_name' WHERE id=$id" ;
            if ($dbp->query($update)) {
                redirect(BASE_URL . 'admin/phone.php');
            }
        }
}

function delete(){
    global $twig, $dbp;
        $id = intval($_GET['phone_id']);

        $delete = "DELETE FROM phone WHERE id=$id";
        if ($dbp->query($delete)) {
            redirect(BASE_URL . 'admin/phone.php');
        }
}

function index(){
    global $twig, $dbp;
    
        $phone = $dbp->get_results("SELECT * FROM phone");

        echo $twig->render('phone.html', array('phone' => $phone));
        
}

function save_phone_detail($phone_id, $phone_details) {
      global $dbp;
      
    foreach($phone_details['option'] as $key => $val) {
        $query = "INSERT INTO   phone_detial(phone_id, `key`, value) VALUES({$phone_id}, '{$key}', '{$val}')";
        $dbp->query($query);
    }
}
?>
